package com.zheng.study.web.controller.main.company;

import com.zheng.study.base.bean.BaseEnumManager;
import com.zheng.study.base.bean.search.SearchEnum;
import com.zheng.study.base.bean.search.order.Order;
import com.zheng.study.base.bean.search.result.SearchResultPaging;
import com.zheng.study.base.bean.search.rule.Rule;
import com.zheng.study.base.bean.search.rule.RuleHelper;
import com.zheng.study.company.entity.Department;
import com.zheng.study.company.entity.Post;
import com.zheng.study.company.entity.Power;
import com.zheng.study.company.entity.User;
import com.zheng.study.company.service.DepartmentService;
import com.zheng.study.company.service.PostService;
import com.zheng.study.company.service.PowerService;
import com.zheng.study.web.controller.BaseController;
import com.zheng.study.web.shiro.bind.annotation.CurrentUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 职权
 * ============================================================================
 * author : dzw
 * createDate:  2018/11/6 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main/power")
public class PowerController extends BaseController {
	private static final long serialVersionUID = 3254371050404526725L;

	@Autowired
	private PowerService powerService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private PostService postService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "list_department", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> list_department(@CurrentUser User user, String departmentId, Integer draw){
		Order order = Order.order("sortNo", SearchEnum.OrderType.asc);
		List<Rule> rules = new ArrayList<Rule>();
		rules.add(RuleHelper.eq("state", BaseEnumManager.StateEnum.Enable));
		rules.add(RuleHelper.eq("company", user.getCompany()));
		rules.add(RuleHelper.eq("department.id", departmentId));
		SearchResultPaging searchResult = powerService.findPagingBySearch(null, rules, order);
		List<Power> powerList = searchResult.getList();
		List<Map<String, Object>> dataRows = new ArrayList<Map<String, Object>>();
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> rMap;
		for(Power power:powerList){
			rMap = new HashMap<String, Object>();
			Department department = power.getDepartment();
			Post post = power.getPost();
			rMap.put("id", power.getId());
			rMap.put("sortNo", power.getSortNo()!=null ? power.getSortNo() : "");
			rMap.put("name", department!=null && post!=null ? department.getName() + " - " +post.getName() : "");
			Power parentPower = power.getParent();
			if(parentPower!=null){
				rMap.put("parentName", parentPower.getDepartment()!=null && parentPower.getPost()!=null ? parentPower.getDepartment().getName() + " - " + parentPower.getPost().getName() : "");
			}else{
				rMap.put("parentName", "");
			}
			dataRows.add(rMap);
		}

		data.put("data", dataRows);
		data.put("draw", draw);
		data.put("recordsTotal", searchResult.getTotalCount());
		data.put("recordsFiltered", searchResult.getTotalCount());
		return data;
	}

	/**
	 * input dialog
	 */
	@RequestMapping(value = "input", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
	public String input(@CurrentUser User user, Model model, String keyId, String departmentId){
		if(StringUtils.isNotEmpty(keyId)){
			Power power = powerService.get(keyId);
			model.addAttribute("power", power);
			model.addAttribute("department", power.getDepartment());
		}else{
			model.addAttribute("department", departmentService.get(departmentId));
		}
		model.addAttribute("postList", postService.getPostList(user.getCompany()));
		return "/main/company/power_input";
	}

	/**
	 * power tree
	 */
	@RequestMapping(value = "tree_level", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Object tree_level(@CurrentUser User user, String departmentId, String powerId){
		Department department = departmentService.get(departmentId);
		Power power = powerService.get(powerId);
		return ajaxJson(resultService.successWithData(powerService.getLevelTreeMapList(user.getCompany(), department, power)));
	}

	/**
	 * tree (人员职权设置)
	 */
	@RequestMapping(value = "tree", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public List<Map<String, Object>> tree(@CurrentUser User user, String userId){
		User doUser = userService.get(userId);
		return powerService.getLevelTreeMapList(user.getCompany(), doUser);
	}

	/**
	 * 保存操作
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(@CurrentUser User user, String keyId, String departmentId, String postId, String parentId, Integer sortNo){
		if (StringUtils.isBlank(departmentId)) {
			return resultService.fail("部门不能为空");
		}
		if (StringUtils.isBlank(postId)) {
			return resultService.fail("岗位不能为空");
		}

		Power power;
		if(StringUtils.isNotBlank(keyId)){
			power = powerService.get(keyId);
		}else{
			power = new Power();
			power.setCompany(user.getCompany());
		}
		power.setSortNo(sortNo);
		power.setDepartment(departmentService.get(departmentId));
		power.setPost(postService.get(postId));
		power.setParent(powerService.get(parentId));
		if(StringUtils.isNotBlank(keyId)){
			powerService.update(power);
		}else{
			keyId = powerService.save(power);
		}

		return ajaxJson(resultService.succesWithId(keyId));
	}

}
